﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace LeRenard.ScriptWars.Model.Scripting
{
    /// <summary>
    /// Concrete implementation of <see cref="IAlgorithm"/>.
    /// </summary>
    public sealed class Algorithm : IAlgorithm
    {
        #region Properties
        /// <summary>
        /// Gets the number of lines the script has.
        /// </summary>
        public int LineCount { get { return Operations.Count(); } }

        /// <summary>
        /// Gets the <see cref="IOperation"/>s defined in the script.
        /// </summary>
        public IEnumerable<IOperation> Operations { get { return new ReadOnlyCollection<IOperation>(_operations); } }
        private readonly IList<IOperation> _operations;
        #endregion

        #region Construction
        /// <summary>
        /// Creates a new <see cref="Algorithm"/> object.
        /// </summary>
        /// <param name="operations">Holds the list of operations</param>.
        public Algorithm(IEnumerable<IOperation> operations)
        {
            ThrowIf.Null(() => operations);

            _operations = operations
                .OrderBy(o => o.LineNumber)
                .ToList();
        }
        #endregion
    }
}
